# 13. 转盘寿司

13 13-1

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

rl.on('line', (line) => {
    const prices = line.split(' ').map(Number);
    const n = prices.length;
    const res = [];
    const stack = [];
    for(let i=0; i<2*n-1; i++) {
        let index = i % n;
        while(stack.length && prices[stack[stack.length - 1]] > prices[index]) {
            let popIndex = stack.pop();
            res[popIndex] = prices[popIndex] + prices[index];
        }
        if (i < n) {
            stack.push(index);
        }
    }

    while(stack.length > 0) {
        const topIndex = stack.pop();
        res[topIndex] = prices[topIndex];
    }

    console.log(res.join(' '));
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30